namespace imitateMacQQ.WebApi.Api.WebSocket;

public class Utils
{
    /// <summary>
    /// 验证ws 身份令牌
    /// </summary>
    /// <param name="token">token</param>
    /// <returns>结果</returns>
    public async Task<bool> GetAuth(string token)
    {
        try
        {
            var port = _configuration.GetSection("Port").Value ?? "7207";
            var url = $"https://localhost:{port}/ws";
            using var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Accept", "text/plain");
            client.DefaultRequestHeaders.Add("Authorization", token);
            var res = await client.GetStringAsync(url);
            return res == "1";
        }
        catch
        {
            return false;
        }
    }

    private readonly IConfiguration _configuration;

    public Utils(IConfiguration configuration)
    {
        _configuration = configuration;
    }
}